
module.exports = {
    /*  
    *   input: (数据集, 标签集, 分割数量)
    *   output： [test_data, test_labels, train_data, train_labels]
    */
    sample: (data, label, num, del=true) => {
        var labelShuffled = label.slice(0), dataShuffled = data.slice(0), i = label.length, min = i - num, labelTemp, dataTemp, index;
        while (i-- > min) {
            index = Math.floor((i + 1) * Math.random());
            labelTemp = labelShuffled[index];
            labelShuffled[index] = labelShuffled[i];
            labelShuffled[i] = labelTemp;
            dataTemp = dataShuffled[index];
            dataShuffled[index] = dataShuffled[i];
            dataShuffled[i] = dataTemp;
            if (del) {
                label.splice(index, 1)
                data.splice(index, 1)
            }
        }
        return {test_data: dataShuffled.slice(min), test_labels: labelShuffled.slice(min), train_data: data, train_labels: label}
    },
    /*
    *   归一化
    *   return (x - min) / (max - min)
    */
    normlize: array => {
        var nums = []
        for (var i = 0; i < array.length; i ++) {
            for (var j = 0; j < array[i].length; j ++) {
                var value =  parseFloat(array[i][j])
                if(!nums[j]) {
                    nums[j] = {min: array[i][j], max: array[i][j]}
                }
                if (nums[j].min > value) {
                    nums[j].min = array[i][j]
                }
                if(nums[j].max < value) {
                    nums[j].max = array[i][j]
                }
            }
        }
        for (var i = 0; i < array.length; i ++) {
            for (var j = 0; j < array[i].length; j ++) {
                array[i][j] =  (parseFloat(array[i][j]) - nums[j].min) / (nums[j].max - nums[j].min)
            }
        }
        return array
    }
}